﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

using EQEmu.Database;

namespace EQEmu.Grids
{
    public class GridCollectionChangedEventArgs : EventArgs
    {
        public GridCollectionChangedEventArgs(IEnumerable<Grid> added, IEnumerable<Grid> removed)
        {
            Added = added;
            Removed = removed;
        }

        public IEnumerable<Grid> Added { get; private set; }
        public IEnumerable<Grid> Removed { get; private set; }
    }

    public class ZoneGrids
    {
        private int _zoneId;
        protected ObservableCollection<Grid> _grids = new ObservableCollection<Grid>();

        public event EventHandler<GridCollectionChangedEventArgs> GridCollectionChanged;

        public ZoneGrids(int zoneId)
        {
            _zoneId = zoneId;
            _grids.CollectionChanged += _grids_CollectionChanged;
            //_zoneShortName = zoneName;          
        }

        public IEnumerable<Grid> Grids
        {
            get { return _grids; }
        }

        public IEnumerable<grid> GridObjects
        {
            get
            {
                foreach (var g in _grids)
                {
                    yield return g.GridValue;
                }
            }
        }

        void _grids_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            var newitems = e.NewItems != null ? e.NewItems.Cast<Grid>() : null;
            var remitems = e.OldItems != null ? e.OldItems.Cast<Grid>() : null;
            OnGridCollectionChanged(newitems, remitems);
        }

        private void OnGridCollectionChanged(IEnumerable<Grid> added, IEnumerable<Grid> removed)
        {
            var e = GridCollectionChanged;
            if (e != null)
            {
                e(this, new GridCollectionChangedEventArgs(added, removed));
            }
        }

        public int ZoneId
        {
            get { return _zoneId; }
        }

        public virtual void AddGrid(Grid grid)
        {
            _grids.Add(grid);
        }

        public virtual void RemoveGrid(Grid grid)
        {
            _grids.Remove(grid);
        }

        public virtual Grid CreateGrid()
        {
            var grid = new grid();
            grid.zoneid = _zoneId;
            return new Grid(grid);
        }
    }
}
